package com.database.service.impl;


import com.database.character.Post;
import com.database.mapper.PostMapper;
import com.database.service.PostService;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

  @Autowired
  private PostMapper postMapper;

  @Override
  public List<Post> getPostBySearch(String search) {
    return postMapper.getPostBySearch(search);
  }

  @Override
  public List<Post> list() {
    return postMapper.list();
  }

  @Override
  public List<Post> getPostByUid(Integer userId) {
    return postMapper.getPostByUid(userId);
  }

  @Override
  public int addPost(Post post) {
    post.setCreateTime(LocalDateTime.now());
    post.setUpdateTime(LocalDateTime.now());
//    解析content字段，解析html提取文本，并将文本添加到searchContent字段
    String content = post.getContent();
//    用jsoup库解析html并提取文本
    String searchContent = Jsoup.parse(content).text();
    post.setSearchContent(searchContent);
    return postMapper.addPost(post);
  }

  @Override
  public int deletePostByPid(Integer postId) {
    return postMapper.deletePostByPid(postId);
  }

  @Override
  public int updatePost(Post post) {
    post.setUpdateTime(LocalDateTime.now());
    return postMapper.updatePost(post);
  }

  @Override
  public Post getPostByPid(Integer postId) {
    return postMapper.getPostByPid(postId);
  }

  @Override
  public List<Post> getAttention(Integer userId) {
    return postMapper.getAttention(userId);
  }


}
